import axios from 'axios'
import qs from 'qs'

// 增加响应拦截器
// var responseInterceptor =
axios.interceptors.response.use(function (response) {
  // Do something with response data
  var code = response.data
  if (code === 1001) {
    // window.location.href = 'http://36.112.137.58:9090/front-cas/login'
    window.app.$router.push({ name: 'login' })
    window.app.$message({
      message: '请先登录!',
      type: 'warning'
    })
    return
  }
  if (code === 1002) {
    window.app.$message({
      message: '无权限访问!',
      type: 'warning'
    })
    return
  }
  if (code === 1003) {
    window.app.$message({
      message: '服务器响应错误请联系管理员!',
      type: 'warning'
    })
    return
  }
  if (code === 1004) {
    window.app.$message({
      message: '用户未同步到本系统,请联系管理员!',
      type: 'warning'
    })
    return
  }
  if (code === 1005) {
    window.app.$message({
      message: '单点登陆失败,请刷新页面重试!',
      type: 'warning'
    })
    return
  }
  if (code === 1009) {
    window.app.$router.push({ name: 'login' })
    window.app.$message({
      message: '该用户已在其他端登录，请重新登录!',
      type: 'warning'
    })
    // window.location.href = code.url
    return
  }
  return response
}, function (error) {
  // Do something with response error
  return Promise.reject(error)
})

// 移除请求拦截器
// axios.interceptors.response.eject(responseInterceptor)

// axios(config)
export default {
  // get 请求
  get (url, params) {
    let urlstr = 'dr' + url
    return axios.get(urlstr, params)
  },
  // post 请求
  post (url, params) {
    axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
    let stringifiedParams = qs.stringify(params)
    url = 'dr' + url
    return axios.post(url, stringifiedParams)
  },
  // post 请求
  postImg (url, params) {
    axios.defaults.headers.post['Content-Type'] = 'multipart/form-data'
    url = 'dr' + url
    return axios.post(url, params)
  }
}
